<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Search extends CI_Controller {
    private $page;
    public function __construct() {
        parent::__construct();
        $user_data = $this->session->all_userdata();
        if (isset($user_data["logged_in"]) && $user_data["logged_in"] == TRUE) {
            $this->page["header"] = $this->load->view("part/header_login", '', true);
        } else {
            $this->page["header"] = $this->load->view("part/header_no_login", '', true);
        }
    }

    /**
     * Action   :index
     * Purpose  :Show the search form with its options
     * Date     :28/03/2012
     * Author   :Pham Truong
     */
    public function index() {
        $page = $this->page;
        $text = $this->input->get("text");
        if ($text == "") {
            $data['erro'] = 'Xin nhập tiêu đề !!';
            $page['content'] = $this->load->view("page/search/search", $data, true);
            $this->load->view("layout", $page);
        } else {
            $type = $this->input->get("select");
            if ($type == "learning_path") {
                $this->path($text);
            } elseif ($type == "member") {
                $this->member($text);
            } elseif ($type == "video") {

                $this->video($text);
            } elseif ($type="article") {
                $this->article($text);
            } else {}
        }
    }

    public function article($text) {
        $page = $this->page;
        $this->load->model("search_model");
        $temp_data['bai'] = $this->search_model->article($text);
        $data['count'] = count($temp_data['bai']);
        if (count($temp_data['bai']) == 0)
            $data['erro'] = 'Không tìm thấy kết quả tiêu đề bạn đã nhập !!!!';
        $data['result'] = $this->load->view('page/search/article', $temp_data, true);
        $page['content'] = $this->load->view("page/search/search", $data, true);
            $this->load->view("layout", $page);
       
    }
    
    /**
     * Action   :path
     * Purpose  :Search all paths have title like #keyword
     * Date     :
     * Author   :
     */
    public function path($text) {
        $page = $this->page;
        $this->load->model("search_model");
        $temp_data['bai'] = $this->search_model->learning_path($text);
        $title_is_null = ($temp_data['bai'][0]['title'] == null);
        /**
         * Gotcha!! avg() always return 1 null record.
         */
        $data['count'] = count($temp_data['bai']);
        if (count($temp_data['bai']) == 0  || $title_is_null) {
            print_r($temp_data['bai']);
            $data['erro'] = 'Không tìm thấy kết quả tiêu đề bạn đã nhập !!!!';
        }
        $data['result'] = $this->load->view('page/search/learning_path', $temp_data, true);
        $page['content'] = $this->load->view("page/search/search", $data, true);
            $this->load->view("layout", $page);
    }

    /**
     * Action   :article
     * Purpose  :Search all articles have title like #keyword
     * Date     :
     * Author   :
     */
    public function video($text) {
        $page = $this->page;
        $this->load->model("search_model");
        $temp_data['bai'] = $this->search_model->video($text);
         $data['count'] = count($temp_data['bai']);
        if (count($temp_data['bai']) == 0)
            $data['erro'] = 'Không tìm thấy kết quả tiêu đề bạn đã nhập !!!!';
        $data['result'] = $this->load->view('page/search/video', $temp_data, true);
        $page['content'] = $this->load->view("page/search/search", $data, true);
            $this->load->view("layout", $page);
    }

    /**
     * Action   :video
     * Purpose  :Search all videos have title like #keyword
     * Date     :
     * Author   :
     */
    public function member($text) {
        $page = $this->page;
        $this->load->model("search_model");
        $temp_data['bai'] = $this->search_model->member($text);
         $data['count'] = count($temp_data['bai']);
        if (count($temp_data['bai']) == 0)
            $data['erro'] = 'Không tìm thấy kết quả tiêu đề bạn đã nhập !!!!';
        $data['result'] = $this->load->view('page/search/member', $temp_data, true);
        $page['content'] = $this->load->view("page/search/search", $data, true);
            $this->load->view("layout", $page);
    }

    /**
     * Action   :member
     * Purpose  :Search all members have title like #keyword
     * Date     :
     * Author   :
     */
}

/* End of file search.php */
/* Location: ./application/controllers/search.php */